<?php 

$config['photo_path'] = '../photos/'; //default path for storing images
$config['thumbnail_path'] = '../photos/thumb/';


function visitProdcut( $pid, $old_vist_num )
{
	$v_num = $old_vist_num + 1;
	$sql = "UPDATE PRODUCTS SET visit_num = " . $v_num . " WHERE id = " . $pid;
	
	return mysql_query($sql);
}

function imageUploading( $name, $type, $size, $tmp_name, $photo_path, $template_path , $thumb_width, $thumb_height )
{

	$image_name = '';
	$extension = '';
	global $config;
	
	 $errors=0;
	 $errorDescription='';
	 
	//checks if the form has been submitted
	 if(true) 
	 {

		//if it is not empty
		if ($name) 
		{
			//get the original name of the file from the clients machine
			$filename = stripslashes($name);
			
			//get the extension of the file in a lower case format
			$extension = getExtension($filename);
			$extension = strtolower($extension);
			
			//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
			//otherwise we will do more tests
			 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
			 {
				//print error message
				$errorDescription += 'Unknown extension!/n';
				$errors=1;
			 }
			 else
			 {
				//get the size of the image in bytes
				 //$_FILES['image']['tmp_name'] is the temporary filename of the file
				 //in which the uploaded file was stored on the server
				 $size=filesize($tmp_name);
		
				//compare the size with the maxim size we defined and print error if bigger
				if ($size > MAX_SIZE*1024)
				{
					$errorDescription += 'You have exceeded the size limit!';
					$errors=1;
				}
		
				//we will give an unique name, for example the time in unix time format
				$image_name=time().'.'.$extension;
				
				//the new name will be containing the full path where will be stored (images folder)
				$newname=$photo_path.$image_name;
				
				
				//we verify if the image has been uploaded, and print error instead
				$copied = copy($tmp_name, $newname);
				
				if (!$copied) 
				{
					$errorDescription += 'Copy unsuccessfull!';
					$errors=1;
				}
				
				//====== Copied sucessfully =======
				//====== Creating thumb =========
				$width = "";
				$height = "";
				list($width, $height) = getimagesize($newname);
				
				if ( $thumb_width == '' )
					$thumb_width = 140;
				if ( $thumb_height == '' )
					$thumb_height = 130;	
								
				jpeg_image($newname, $image_name , $extension , $width, $height,$thumb_width,$thumb_height,$template_path);
			
				
					
					
					
				
			}
		}
		
		
	 }
	
	//If no errors registred, print the success message
	 if(!$errors) 
	 {
		echo "<h1>File Uploaded Successfully! Try again!</h1>";
	 }
	 
	 return $image_name;
	
}

// This function is responsible for creating thumbs, it is being called by the imgaeUploading function
function jpeg_image($orginal_image_location, $orginal_image_name, $orginal_image_extension, $orginal_width, $orginal_height,$new_width,$new_height,$path ){
	
	 if (($orginal_image_extension == "jpg") || ($orginal_image_extension == "jpeg") || ($orginal_image_extension == "png") || ($orginal_image_extension == "gif")) 
	 {
		
		
		switch( $orginal_image_extension )
		{
			case 'jpg':
			case 'jpeg':
			$img = imagecreatefromjpeg($orginal_image_location);
			break;
			
			case 'gif':
			$img = imagecreatefromgif($orginal_image_location);
			break;
			
			case 'png':
			$img = imagecreatefrompng($orginal_image_location);
			break;
			
			
		}
		$image_p = imagecreatetruecolor($new_width,$new_height);
		imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $orginal_width, $orginal_height);
		$thenames=$path.$orginal_image_name;	
		
		if ( $orginal_image_extension == "jpeg" || $orginal_image_extension == "jpg" )
			imagejpeg($image_p,$thenames, 100);
		else if ( $orginal_image_extension == "gif" )
			imagegif($image_p,$thenames, 100);
		else if ( $orginal_image_extension == "png" )
			imagepng($image_p,$thenames, 9);
		
	 }
	 else
	 {
		// echo 'unsupported image type .';
	 }
		
}

 function getExtension($str) {
		 $i = strrpos($str,".");
		 if (!$i) { return ""; }
		 $l = strlen($str) - $i;
		 $ext = substr($str,$i+1,$l);
		 //echo $ext;
		 return $ext;
 }

?>